﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceApplication.Dao.WorkFlow;
using CommonLibrary.DataModel.WorkFlow;
using CommonLibrary.Common;
using CommonLibrary.DataModel.Basic;
using CommonLibrary.Wf.InterFace;
using CommonLibrary.Wf.Common;

namespace ServiceApplication.Service.WorkFlow
{

    /// <summary>
    /// IBILLService 实现，本类在 web项目 <ExternalDataExchangeService/> 节配置加载
    /// </summary>
    [Serializable]
    public class BillServer : IBILLService
    {
        public void Info2Next(Guid instanceID, UserEntity fqrxx, ChoiceClrxx clrxx, 
            string taskName, object wivList)
        {
            WfInsvalueDao wivDAO = new WfInsvalueDao();
            string fysj = DateUtil.getStandardTimeBySecond();
            for (int i = 0; i < clrxx.处理人编码.Length; i++)
            {
                WfTaskEntity wfTask = new WfTaskEntity();
                wfTask.uniqueid = instanceID;
                wfTask.taskname = taskName;
                wfTask.state = WfTaskState.TASK_UNDO;
                wfTask.processresult = "";
                wfTask.processtype = clrxx.处理者类型;
                wfTask.processaddition = "";
                wfTask.user_id = clrxx.处理人编码[i];
                wfTask.fqrcode = fqrxx.user_id;
                wfTask.starttime = fysj;
                wfTask.checkintime = "";
                wfTask.endtime = DateUtil.getStandardTimeBySecond();
                wfTask.days = 100;

                string taskid = TaskDao.Add(wfTask);
                foreach (var item in (List<WfInsvalueEntity>)wivList)
                {
                    item.task_id = int.Parse(taskid);
                }
                wivDAO.AddEntity((List<WfInsvalueEntity>)wivList);
            }
        }

        Dictionary<string, EventHandler<ReceivedEventArgs>> _EventList = new Dictionary<string, EventHandler<ReceivedEventArgs>>();

        public void RaiseEvent(string name, ReceivedEventArgs receivedEventArgs)
        {
            receivedEventArgs.WaitForIdle = true;
            if (_EventList[name] != null)
            {
                EventHandler<ReceivedEventArgs> eventHand = _EventList[name];
                eventHand(this, receivedEventArgs);
            }
        }

        public static void updateTask(WfTaskEntity wftaskEntity)
        {
            TaskDao.Update(wftaskEntity);
        }

        #region IBILLService 成员
        public event EventHandler<ReceivedEventArgs> BillUpdated
        {
            add { _EventList.Add("BillUpdated", value); }
            remove { _EventList.Remove("BillUpdated"); }
        }
        public event EventHandler<ReceivedEventArgs> BillNext
        {
            add { _EventList.Add("BillNext", value); }
            remove { _EventList.Remove("BillNext"); }
        }
        public event EventHandler<ReceivedEventArgs> BillCanceled
        {
            add { _EventList.Add("BillCanceled", value); }
            remove { _EventList.Remove("BillCanceled"); }
        }
        public event EventHandler<ReceivedEventArgs> BillWait
        {
            add { _EventList.Add("BillWait", value); }
            remove { _EventList.Remove("BillWait"); }
        }
        public event EventHandler<ReceivedEventArgs> BillInsert
        {
            add { _EventList.Add("BillInsert", value); }
            remove { _EventList.Remove("BillInsert"); }
        }
        #endregion

        #region 公共成员
        //public event EventHandler<MessageReceivedEventArgs> MessageReceived;
        #endregion
    }
}
